﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using LucasCode.PublicTypes;
using LucasCode.Tools;
using ESLReminder.Games;
using UserNotifier;
using System.Threading;
using LucasCode.Win32;
using System.ComponentModel;

namespace ESLReminder.Modules
{
    public class ScreenshotModule : ReminderModule
    {
        FileSystemWatcher ScreenshotWatcher;
        public ScreenshotModule()
            : base(new PluginInfo("ScreenshotModule",
                                    new Version(1,0),
                                    "©2009 Lucas Romero",
                                    "http://code.google.com/p/quicktools/"),
                    "Watches out for screenshots being made in order to make them available for uploading.")
        {
            ScreenshotWatcher = new FileSystemWatcher();
            ScreenshotWatcher.Created += new FileSystemEventHandler(ScreenshotWatcher_Created);
            ScreenshotWatcher.EnableRaisingEvents = false;
            ScreenshotWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime;
        }
        public override void PluginLoadedEx(IReminderInterface host)
        {
            host.GameStarted += new GameStartedEventHandler(reminder_GameStarted);
            host.GameEnded += new GameEndedEventHandler(reminder_GameEnded);
        }

        private bool m_RemindToTakeStatusSnapshot = true;
        //TODO: rename this
        [DefaultValueAttribute(true)]
        public bool RemindToTakeStatusSnapshot
        {
            get
            {
                return m_RemindToTakeStatusSnapshot;
            }
            set
            {
                m_RemindToTakeStatusSnapshot = value;
            }
        }

        void reminder_GameEnded(IReminderInterface sender, GameEndedEventArgs args)
        {
            ScreenshotWatcher.EnableRaisingEvents = false;
        }

        void ScreenshotWatcher_Created(object sender, FileSystemEventArgs e)
        {
            if (!m_Reminder.CurrentArchive.containsFile(e.FullPath))
                m_Reminder.CurrentArchive.AddFile(new MatchFile(e.FullPath));
        }

        void reminder_GameStarted(IReminderInterface sender, GameStartedEventArgs args)
        {
            if (m_RemindToTakeStatusSnapshot)
                m_Reminder.AddWarning(WarnEvents.TakeStatus, Timeout.Infinite);
            try
            {
                string executabledir = Path.GetDirectoryName(args.Game.Process.MainModule.FileName);
                Type game = args.Game.GetType();
                if (game == typeof(GameHalfLife))
                {
                    ScreenshotWatcher.Path = Path.Combine(executabledir, GameInteropModule.getModFromDir(executabledir));
                    ScreenshotWatcher.Filter = "*.bmp";
                }
                else if (game == typeof(GameHalfLife2))
                {
                    ScreenshotWatcher.Path = Path.Combine(Path.Combine(executabledir, GameInteropModule.getModFromDir(executabledir)),"screenshots");
                    ScreenshotWatcher.Filter = "*.jpg";
                }
                else if (game == typeof(GameWarcraft3))
                {
                    ScreenshotWatcher.Path = Path.Combine(executabledir, "Screenshots");
                    ScreenshotWatcher.Filter = "*.tga";
                }
                else
                {
                    throw new NotSupportedException("Game " + game.Name + " not supported");
                }
                ScreenshotWatcher.EnableRaisingEvents = true;
            }
            catch (Exception ex)
            {
                LogError("Could not initialize: " + ex.Message);
            }
        }
    }
}
